package org.lc.platform.base.util;

import cn.hutool.json.JSONUtil;
import org.lc.platform.base.domain.LoggerSign;
import org.lc.platform.base.enums.LogCategoryEnum;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class LoggerUtil {

    public static String createEncoderLogger(String userId, String category, Boolean saveRes) {
        Map<String, Object> maps = new HashMap<>();
        maps.put(LogCategoryEnum.SAVE_LOG.getValue(), true);
        maps.put(LogCategoryEnum.USER_ID.getValue(), userId);
        maps.put(LogCategoryEnum.CATEGORY.getValue(), category);
        maps.put(LogCategoryEnum.ENABLED_RESPONSE.getValue(), saveRes);
        var jsonStringBytes = JSONUtil.toJsonStr(maps).getBytes();
        return Base64.getEncoder().encodeToString(jsonStringBytes);
    }

    public static LoggerSign decoderLoggerStr(String logger) {
        try {
            var strMap = Base64.getDecoder().decode(logger);
            var json = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(strMap));
            return JSONUtil.toBean(JSONUtil.parseObj(json), LoggerSign.class);
        } catch (Exception ignored) {
        }
        return new LoggerSign();
    }
}
